/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import entreprise.Patient;
import entreprise.PatientService;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author duonghau
 */
public class PatientBean {
    private String patientID="";
    private String patientName="";
    private String patientSex="";
    private Date patientDateOfBirth;
    private String patientJob="";
    private String patientAddress="";
    private String patientPhoneNumber="";
    private String patientEmail="";
    private float patientHeight;
    private float patientWeight;
    private String patientBloodType=""; 
    private String message="";
    private String messageStyle="";
    private String patientSearchName="";
    private int page=1;
    private int numberOfRowPerPage=10;    
    
    public String getPatientID() {
        return patientID;
    }

    public void setPatientID(String patientID) {
        this.patientID = patientID;
    }

    public String getPatientName() {
        return patientName;
    }

    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }

    public String getPatientSex() {
        return patientSex;
    }

    public void setPatientSex(String patientSex) {
        this.patientSex = patientSex;
    }

    public Date getPatientDateOfBirth() {
        return patientDateOfBirth;
    }

    public void setPatientDateOfBirth(Date patientDateOfBirth) {
        this.patientDateOfBirth = patientDateOfBirth;
    }

    public String getPatientJob() {
        return patientJob;
    }

    public void setPatientJob(String patientJob) {
        this.patientJob = patientJob;
    }

    public String getPatientAddress() {
        return patientAddress;
    }

    public void setPatientAddress(String patientAddress) {
        this.patientAddress = patientAddress;
    }

    public String getPatientPhoneNumber() {
        return patientPhoneNumber;
    }

    public void setPatientPhoneNumber(String patientPhoneNumber) {
        this.patientPhoneNumber = patientPhoneNumber;
    }

    public String getPatientEmail() {
        return patientEmail;
    }

    public void setPatientEmail(String patientEmail) {
        this.patientEmail = patientEmail;
    }

    public float getPatientHeight() {
        return patientHeight;
    }

    public void setPatientHeight(float patientHeight) {
        this.patientHeight = patientHeight;
    }

    public float getPatientWeight() {
        return patientWeight;
    }

    public void setPatientWeight(float patientWeight) {
        this.patientWeight = patientWeight;
    }

    public String getPatientBloodType() {
        return patientBloodType;
    }

    public void setPatientBloodType(String patientBloodType) {
        this.patientBloodType = patientBloodType;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessageStyle() {
        return messageStyle;
    }

    public void setMessageStyle(String messageStyle) {
        this.messageStyle = messageStyle;
    }

    public String getPatientSearchName() {
        return patientSearchName;
    }

    public void setPatientSearchName(String patientSearchName) {
        this.patientSearchName = patientSearchName;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }    
    
    
    public ArrayList<Patient> getPatients(){
        PatientService patientService=new PatientService();
        ArrayList<Patient> patients=patientService.patients(patientSearchName,(page-1)*numberOfRowPerPage,numberOfRowPerPage);
        return patients;
    }
    
    public ArrayList<Integer> getPagings(){
        PatientService patientService=new PatientService();        
        int numberOfRow=patientService.getNumberOfPatients(patientSearchName);
        int numberOfPage=(numberOfRow/numberOfRowPerPage);
        if(numberOfRow%numberOfRowPerPage!=0){
            numberOfPage+=1;
        }
        ArrayList<Integer> paging=new ArrayList<>();
        for(int i=1;i<=numberOfPage;i++){
            paging.add(i);
        }
        return paging;        
    }
    
    public void getPatientDetail(){
        if(patientID!=null){
            PatientService patientService=new PatientService();
            Patient patient = patientService.getPatient(patientID);
            if(patient!=null){
                patientName=patient.getPatientName();
                patientSex=patient.getPatientSex();
                patientDateOfBirth=patient.getPatientDateOfBirth();
                patientJob=patient.getPatientJob();
                patientAddress=patient.getPatientAddress();
                patientPhoneNumber=patient.getPatientPhoneNumber();
                patientEmail=patient.getPatientEmail();
                patientHeight=patient.getPatientHeight();
                patientWeight=patient.getPatientWeight();
                patientBloodType=patient.getPatientBloodType();
            }
        }         
    }
    public String addPatient(){
        PatientService patientService=new PatientService();
        if(patientService.createPatient("", patientName, patientSex, patientDateOfBirth, patientJob, patientAddress, patientPhoneNumber, patientEmail,patientHeight, patientWeight, patientBloodType)){
            message="Patient add success";
            messageStyle="success";
            String patientIDString=String.valueOf(patientService.getLatestPatientID());
            return "adduser.xhtml?faces-redirect=true&patientID="+patientIDString+"&userEmail="+patientEmail;
        }else{
            message="Patient add error";
            messageStyle="error";
            return "patients";
        }
    }
    
    public String editPatient(){
        PatientService patientService=new PatientService();
        if(patientService.updatePatient(patientID, patientName, patientSex, patientDateOfBirth, patientJob, patientAddress, patientPhoneNumber, patientEmail, patientHeight, patientWeight, patientBloodType)){
            message="Patient update success";
            messageStyle="success";               
        }else{
            message="Patient update error";
            messageStyle="error";              
        }        
        return "patients";
    }
    
    public String deletePatient(String patientID){
        PatientService patientService=new PatientService();
        if(patientService.deletePatient(patientID)){
            message="Patient delete success";
            messageStyle="success";              
        }else{
            message="Patient delete error";
            messageStyle="error";   
        }   
        return "patients";
    }
}
